import base64, json, hashlib
# from django.conf import settings

if __name__ == '__main__':

    # 头部
    header = {"typ": "jwt", "alg": "HS256"}
    header_str = base64.b64encode(json.dumps(header).encode()).decode()

    # 载荷
    preload = {"sub": "1234567890", "exp": "150123455", "name": "John Doe", "admin": True,
               "acc_pwd": "QiLCJhbGciOiJIUzI1NiJ9", }
    preload_str = base64.b64encode(json.dumps(preload).encode()).decode()

    # 签证
    # secret = settings.SECRET_KEY
    # 签证的核心就是秘钥，秘钥不能提供给客户端，也就是客户端因为没有秘钥，所以即便拿到了头部和载荷，也没有办法生成jwt token
    secret = 'django-insecure-v=wop&b(%-*6+p4zx%id21h43c@24@bm85g1%#ms^t6zkc396g'
    data = header_str + preload_str + secret
    HS256 = hashlib.sha256()
    HS256.update(data.encode('utf-8'))
    signature = HS256.hexdigest()

    token = f"{header_str}.{preload_str}.{signature}"
    print(token)